<template>
	<div>
		<div style="display: inline-block;float: right;">
			<el-button size="mini" type="primary" icon="document" @click="refresh">刷新</el-button>
			<el-button style='margin:0 0 20px 10px;' :disabled="!isAuth('coupon:add')" size="mini" type="primary"
				icon="document" @click="classifyStair()">添加红包</el-button>
		</div>
		<el-table v-loading="tableDataLoading" :data="tableData.records">
			<!-- <el-table-column type="selection" fixed>
      </el-table-column> @selection-change="changeFun"-->
			<el-table-column prop="redPacketTypeId" label="编号" width="80">
			</el-table-column>
			<el-table-column prop="redPacketTitle" label="红包名称">
			</el-table-column>
			<!-- <el-table-column prop="couponPicture" label="图片">
				<template slot-scope="scope">
					<img :src="scope.row.couponPicture" alt="" width="40" height="40">
				</template>
			</el-table-column> -->
			<el-table-column prop="redPacketType" label="使用类型">
				<template slot-scope="scope">
					<span v-if="scope.row.redPacketType==0">全部通用</span>
					<span v-if="scope.row.redPacketType==1">帮我送</span>
					<span v-if="scope.row.redPacketType==2">帮我取</span>
					<span v-if="scope.row.redPacketType==3">帮我买</span>
					<span v-if="scope.row.redPacketType==4">同城服务</span>
				</template>
			</el-table-column>
			<el-table-column prop="redPacketAmount" label="红包金额">
			</el-table-column>
			<el-table-column prop="minimumAmount" label="最低消费">
			</el-table-column>
			<el-table-column prop="expirationDay" label="有效天数">
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间">
			</el-table-column>
			<el-table-column label="操作" width="250">
				<template slot-scope="scope">
					<el-button style='margin-left:15px;' :disabled="!isAuth('coupon:zengsong')" size="mini" type="primary"
						icon="document" @click="songCoupon(scope.row)">赠送</el-button>
					<el-button size="mini" type="primary" :disabled="!isAuth('coupon:update')"
						@click="compile(scope.$index, scope.row)">编辑
					</el-button>
					<el-button size="mini" type="danger" :disabled="!isAuth('coupon:delete')"
						@click="deleteStair(scope.row)">删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div style="text-align: center;margin-top: 10px;">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:page-sizes="[10, 20, 30, 40]" :page-size="limit" :current-page="page"
				layout="total,sizes, prev, pager, next" :total="tableData.total">
			</el-pagination>
		</div>
		<!-- 添加红包 -->
		<el-dialog title="添加红包" :visible.sync="dialogFormVisible" center>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">红包名称：</span>
				<el-input style="width:50%;" v-model="redPacketTitle" type="text" placeholder="请输入红包名称"></el-input>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">使用类型：</span>
				<el-select v-model="redPacketType" style="width:50%;">
					<el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">红包金额：</span>
				<el-input style="width:50%;" v-model="redPacketAmount" type="text" placeholder="请输入红包金额"></el-input>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">最低消费：</span>
				<el-input style="width:50%;" v-model="minimumAmount" type="text" placeholder="请输入最低消费"></el-input>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">有效天数：</span>
				<el-input style="width:50%;" v-model="expirationDay" type="text" placeholder="请输入有效天数"></el-input>
			</div>

			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="StairNoticeTo()">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 修改一级分类 -->
		<el-dialog title="修改红包" :visible.sync="dialogFormVisible1" center>
			<el-form :model="form">
				<el-form-item label="红包名称：" :label-width="formLabelWidth">
					<el-input v-model="form.redPacketTitle" style="width:65%;"></el-input>
				</el-form-item>
				<el-form-item label="使用类型：" :label-width="formLabelWidth">
					<el-select v-model="form.redPacketType" style="width:65%;">
						<el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="红包金额：" :label-width="formLabelWidth">
					<el-input v-model="form.redPacketAmount" style="width:65%;"></el-input>
				</el-form-item>
				<el-form-item label="最低消费：" :label-width="formLabelWidth">
					<el-input v-model="form.minimumAmount" style="width:65%;"></el-input>
				</el-form-item>
				<el-form-item label="有效天数：" :label-width="formLabelWidth">
					<el-input v-model="form.expirationDay" style="width:65%;"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible1 = false">取 消</el-button>
				<el-button type="primary" @click="CompileNoticeTo()">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 赠送红包-->
		<el-dialog title="赠送红包" :visible.sync="dialogFormVisible0" center>
			<div style="margin: 10px 0;">
				<span style="width: 200px;display: inline-block;text-align: right;">选择用户：</span>
				<el-tag :key="tag" v-for="(tag,j) in userNameList" closable :disable-transitions="false"
					@close="couponIdClose1(tag,j)">
					{{tag}}
				</el-tag>
				<el-button class="button-new-tag" size="small" @click="couponIdBtn1()">添加</el-button>
			</div>

			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible0 = false">取 消</el-button>
				<el-button type="primary" @click="songNoticeTo()">确 定</el-button>
			</div>
		</el-dialog>

		<!-- 用户列表 -->
		<el-dialog title="用户列表" :visible.sync="dialogFormVisible5" center>

			<el-input style="width: 150px;" @keydown.enter.native="select1" clearable placeholder="请输入手机号"
				v-model="phone">
			</el-input>&nbsp;&nbsp;&nbsp;&nbsp;
			<el-input style="width: 150px;" @keydown.enter.native="select1" clearable placeholder="请输入昵称"
				v-model="nickName">
			</el-input>
			<div style="display: inline-block;">
				<el-button style="margin-left:15px;" size="mini" type="primary" icon="document" @click="select1">查询
				</el-button>
				<el-button style="margin-left:15px;" size="mini" type="primary" icon="document" @click="cleans1">重置
				</el-button>
			</div>
			<el-button style="margin-left:15px;" :disabled="xuanzhong.length == 0 " size="mini" type="primary"
				icon="document" @click="closes2()">选择用户</el-button>
			<el-table v-loading="tableDataLoading1" @selection-change="changeFun1" :data="userData.list"  ref="multipleTable"><!-- :row-key="(row)=>{ return row.userId}" -->
				<el-table-column :reserve-selection="true" type="selection" fixed>
				</el-table-column>
				<el-table-column fixed prop="userId" label="编号">
				</el-table-column>
				<el-table-column fixed prop="nickName" label="昵称">
					<template slot-scope="scope">
						<span style="color: #f56c6c;">{{ scope.row.nickName ? scope.row.nickName : '未设置' }}</span>
					</template>
				</el-table-column>
				<el-table-column label="图像">
					<template slot-scope="scope">
						<img v-if="scope.row.avatar==null" src="~@/assets/img/avatar.png" alt="" width="40" height="40">
						<img v-else :src="scope.row.avatar" alt="" width="40" height="40">
					</template>
				</el-table-column>
				<el-table-column prop="phone" label="手机号" width="120">
					<template slot-scope="scope">
						<span style="color: #4f9dec;cursor: pointer;"
							@click="updates(scope.row)">{{ scope.row.phone ? scope.row.phone : '未绑定' }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="platform" label="来源">
				</el-table-column>
				<el-table-column prop="createTime" label="创建时间">
				</el-table-column>
			</el-table>
			<div style="text-align: center;margin-top: 10px;">
				<el-pagination @size-change="handleSizeChange2" @current-change="handleCurrentChange2"
					:page-sizes="[5, 10, 15, 20]" :page-size="size1" :current-page="page1"
					layout="total,sizes, prev, pager, next,jumper" :total="userData.totalCount">
				</el-pagination>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				limit: 10,
				page: 1,
				size1: 10,
				page1: 1,
				checkBoxData: [], //多选框选择的值
				formLabelWidth: '200px',
				tableDataLoading: false,
				dialogFormVisible: false,
				dialogFormVisible1: false,
				dialogFormVisible0: false,
				dialogFormVisible5: false,
				tableDataLoading1: false,
				tableData: [],
				redPacketTitle: '',
				couponPicture: '',
				expirationDay: '',
				minimumAmount: '',
				redPacketAmount: '',
				needIntegral: '',
				form: {
					redPacketTypeId: '',
					redPacketTitle: '',
					expirationDay: '',
					minimumAmount: '',
					redPacketAmount: '',
					redPacketType: ''
				},
				userNameList: [],
				userIdList: [],
				userData: [],
				phone: '',
				nickName: '',
				redPacketTypeId: '',
				content: '',
				typeList: [{
						value: '0',
						label: '全部通用'
					},
					{
						value: '1',
						label: '帮我送'
					}, {
						value: '2',
						label: '帮我取'
					}, {
						value: '3',
						label: '帮我买'
					}, {
						value: '4',
						label: '同城服务'
					}
				],
				redPacketType:'',
				xuanzhong:[],
			}
		},
		methods: {
			// 多选
			changeFun(val) {
				this.checkBoxData = val;
			},
			handleSizeChange(val) {
				this.limit = val;
				this.dataSelect()
			},
			handleCurrentChange(val) {
				this.page = val;
				this.dataSelect()
			},
			handleSizeChange2(val) {
				this.size1 = val;
				this.dataSelect1()
			},
			handleCurrentChange2(val) {
				this.page1 = val;
				this.dataSelect1()
			},
			handleAvatarSuccess1(file, fileList) {
				this.couponPicture = file.data
			},
			handleAvatarSuccess2(file, fileList) {
				this.form.couponPicture = file.data
			},
			// 刷新
			refresh() {
				this.page = 1
				this.dataSelect()
			},
			// 添加红包弹框
			classifyStair() {
				this.redPacketTitle = ''
				this.redPacketType = ''
				this.minimumAmount = ''
				this.redPacketAmount = ''
				this.expirationDay = ''
				this.dialogFormVisible = true
			},
			// 添加红包
			StairNoticeTo() {
				if (this.redPacketTitle == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入红包名称',
						type: 'warning'
					});
					return
				}
				if (this.redPacketType == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请选择使用类型',
						type: 'warning'
					}); 
					return
				}
				if (this.redPacketAmount == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入红包金额',
						type: 'warning'
					});
					return
				}
				if (this.minimumAmount == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入最低消费金额',
						type: 'warning'
					});
					return
				}
				if (this.expirationDay == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入红包有效天数',
						type: 'warning'
					});
					return
				}
				this.$http({
					url: this.$http.adornUrl('adminevaluate/insertRedPacketType'),
					method: 'post',
					data: this.$http.adornData({
						'redPacketTitle': this.redPacketTitle,
						'expirationDay': this.expirationDay,
						'minimumAmount': this.minimumAmount,
						'redPacketAmount': this.redPacketAmount,
						'redPacketType': this.redPacketType
					})
				}).then(({
					data
				}) => {
					if (data.code == 0) {
						this.dialogFormVisible = false
						this.$message({
							message: '添加成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.dataSelect()
							}
						})
					} else {
						this.$message({
							message: data.msg,
							type: 'warning',
							duration: 1500,
							onClose: () => {}
						})
					}
				})
			},
			// 修改红包
			compile(index, rows) {
				this.form.redPacketTitle = rows.redPacketTitle
				this.form.expirationDay = rows.expirationDay
				this.form.minimumAmount = rows.minimumAmount
				this.form.redPacketAmount = rows.redPacketAmount
				this.form.redPacketTypeId = rows.redPacketTypeId
				this.form.redPacketType = rows.redPacketType
				this.dialogFormVisible1 = true;
			},
			// 修改红包
			CompileNoticeTo() {
				this.$http({
					url: this.$http.adornUrl('adminevaluate/updateRedPacketType'),
					method: 'post',
					data: this.$http.adornData({
						'redPacketTypeId': this.form.redPacketTypeId,
						'redPacketTitle': this.form.redPacketTitle,
						'redPacketType': this.form.redPacketType,
						'expirationDay': this.form.expirationDay,
						'minimumAmount': this.form.minimumAmount,
						'redPacketAmount': this.form.redPacketAmount,
					})
				}).then(({
					data
				}) => {
					if (data.code == 0) {
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.dialogFormVisible1 = false
								this.dataSelect()
							}
						})
					} else {
						this.$message({
							message: data.msg,
							type: 'error',
							duration: 1500,
							onClose: () => {
								this.dialogFormVisible1 = false
								this.dataSelect()
							}
						})
					}
				})
			},

			//删除
			deleteStair(row) {
				this.$confirm(`确定删除此条信息?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$http({
						url: this.$http.adornUrl('adminevaluate/deleteRedPacketType'),
						method: 'post',
						params: this.$http.adornParams({
							'redPacketTypeId': row.redPacketTypeId
						})
					}).then(({
						data
					}) => {
						if (data.code == 0) {
							this.$message({
								message: '删除成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.dataSelect()
								}
							})
						} else {
							this.$message({
								message: data.msg,
								type: 'error',
								duration: 1500,
								onClose: () => {
									this.dataSelect()
								}
							})
						}

					})
				}).catch(() => {})
			},
			// 重置
			cleans() {
				this.phone = ''
				this.status = 0
				this.dataSelect()
			},
			// 获取派单数据列表
			dataSelect() {
				this.tableDataLoading = true
				this.$http({
					url: this.$http.adornUrl('adminevaluate/selectRedPacketTypeList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.page,
						'limit': this.limit,
						'redPacketTitle': this.content
					})
				}).then(({
					data
				}) => {
					this.tableDataLoading = false
					let returnData = data.data;
					this.tableData = returnData
					if(this.page>1){
						if (data.data.list.records == 0) {
							this.page = this.page - 1
							this.dataSelect()
						}
					}
					
				})
			},
			// 赠送红包
			songCoupon(row) {
				this.userIdList = []
				this.userNameList = []
				this.redPacketTypeId = row.redPacketTypeId
				this.dialogFormVisible0 = true
			},
			// 打开用户列表
			couponIdBtn1() {
				var userIdList = []
				var userNameList = []
				this.page1 = 1
				this.phone = ''
				this.nickName = ''
				this.dataSelect1()
				
				this.dialogFormVisible5 = true
			},
			// 多选用户
			changeFun1(val) {
				console.log('-----',val)
				this.xuanzhong = val
				
			},
			// 赠送红包
			songNoticeTo() {
				if (this.userIdList.length == 0) {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请选择用户',
						type: 'warning'
					});
					return
				}
				var userIdList = this.userIdList.toString()
				this.$http({
					url: this.$http.adornUrl('adminevaluate/sendRedPacketType'),
					method: 'post',
					params: this.$http.adornParams({
						'userIds': userIdList,
						// 'shopId': 0,
						'redPacketTypeId': this.redPacketTypeId,
					})
				}).then(({
					data
				}) => {
					this.$message({
						message: '操作成功',
						type: 'success',
						duration: 1500,
						onClose: () => {
							this.userIdList = []
							this.userNameList = []
							this.dataSelect()
						}
					})
					this.dialogFormVisible0 = false
				})
			},
			// 获取用户数据列表
			dataSelect1() {
				this.tableDataLoading1 = true
				this.$http({
					url: this.$http.adornUrl('adminerrand/findAllUser'),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.page1,
						'limit': this.size1,
						'shopAdminFlag': '',
						'phone': this.phone,
						'nickName': this.nickName,
						'type': ''
					})
				}).then(({
					data
				}) => {
					this.tableDataLoading1 = false
					let returnData = data.data
					this.userData = returnData
					
				})
			},
			// 查询
			select1() {
				this.page1 = 1
				this.dataSelect1()
			},
			// 重置
			cleans1() {
				this.page1 = 1
				this.phone = ''
				this.nickName = ''
				this.dataSelect1()
			},
			couponIdClose1(ietm,index) {
				if (this.userIdList.length < 2) {
					this.$message({
						title: '提示',
						type: 'error',
						duration: 1800,
						message: '最少需要选中一位用户',
						type: 'warning'
					});
					return
				} else {
					this.userIdList.splice(index, 1)
					this.userNameList.splice(index, 1)
				}
			},
			// 批量开启
			closes2(id) {
				var userIdList = []
				var userNameList = []
				for (var i in this.xuanzhong) {
					if(this.userIdList.length>0){
						this.userIdList.push(this.xuanzhong[i].userId)
					}
					if(this.userIdList.length>0){
						this.userNameList.push(this.xuanzhong[i].nickName)
					}
					userIdList.push(this.xuanzhong[i].userId)
					userNameList.push(this.xuanzhong[i].nickName)
				}
				if(this.userIdList.length==0){
					this.userIdList = userIdList
					this.userNameList = userNameList
				}
				
				this.isCoupon = 1
				
				this.dialogFormVisible5 = false
				this.$refs.multipleTable.clearSelection();
				console.log(this.userNameList)
			},
		},
		mounted() {
			this.dataSelect()
		}
	};
</script>

<style>

</style>
